package cn.zhoujing.demo.redis.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.zhoujing.demo.redis.enity.MyUser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author zhoujing
 * @createTime 2023/11/30 - 18:03
 */
@Component
@Configuration
public class CustomUserDetailsServiceImpl implements UserDetailsService {


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username.equals("user")) {
            MyUser myUser = new MyUser();
            myUser.setUserName("user");
            myUser.setPassword(passwordEncoder().encode("user"));
            myUser.setAuthorities(ListUtil.of(() -> "user"));
            myUser.setEnabled(true);
            myUser.setAccountNonLocked(true);
            myUser.setAccountNonExpired(true);
            myUser.setCredentialsNonExpired(true);
            return myUser;
        } else if (username.equals("admin")) {
            MyUser myUser = new MyUser();
            myUser.setUserName("admin");
            myUser.setPassword(passwordEncoder().encode("admin"));
            myUser.setAuthorities(ListUtil.of(() -> "admin"));
            myUser.setEnabled(true);
            myUser.setAccountNonLocked(true);
            myUser.setAccountNonExpired(true);
            myUser.setCredentialsNonExpired(true);
            return myUser;
        }
        return null;
    }

}
